/**
 * @author Kevin.MOU Login view script Support
 */
(function($) {
	var Login = {
		url : 'http://localhost',
		init : function() {
			var a = this;
			a.rsaPubKey = $('input#rsaPubKey').val();
			var form = $('form.login_form');
			form.on('submit', function(e) {
				e.preventDefault();
				var password = $("input#password", form).val();
				$(".login_err_panel").hide();
				a.doLogin(password);
			});
		},

		doLogin : function(password) {
			var a = this, remoteKey = localStorage.getItem("elite_remote_key");
			if (!remoteKey) {
				remoteKey = Guid.raw();
				localStorage.setItem("elite_remote_key", remoteKey);
			}
			a.onSubmit(password, remoteKey);
		},

		onSubmit : function(password, remoteKey) {
			var a = this;
			busy(1, $F.lang('submit'));
			Ajax.post({
				url : a.url + '/m/quicklogin_',
				data : {
					quickLoginId: a.quickId,
					password: $F.encPwd(password, a),
					uid: a.uid
				},
				success : function(data) {
					//console.log(data);
					if (data.code > 0) {
						$EP.setStaff(data.value, function() {
							location.href = a.url + data.message;
						});
					} else {
						a.showError(data.message);
					}
				},
				always : function() {
					busy(0);
				}
			});
		},

		showError : function(e) {
			$("#err_tips").text(e);
			$(".login_err_panel").show();
		}
	};

	window.Login = Login;

	var busy = function(e, m) {
		if (e == 1) {
			$(".login_btn").hide();
			$(".login_busy").show().find('>span').text(m);
		} else {
			$(".login_busy").hide().find('>span').text('');
			$(".login_btn").show();
		}
	},
	showError = function(e) {
		$("#err_tips").text(e);
		$(".login_err_panel").show();
	};

	$(function() {
		$EP.getUrl(function(result) {
			// console.log(result);
			Login.url = result.url;
			Login.jsv = result.jsv;
			Login.forceLoginAllow = result.forceLoginAllow;
			Login.ehmd2 = result.ehmd2;
			Login.verifyAuth = result.verifyAuth;
			Login.dataService = result.dataService;
			Login.defaultMode = result.defaultMode;
			Login.enc = result.enc;
			Login.pwdEncWithRSA = result.pwdEncWithRSA;
			Login.uid = result.uid;
			Login.language = result.language;
			Login.quickId = result.quickId;
			Login.init();
		}, function() {
			alert("$EP.getUrl error");
		});
	});
})(jQuery);